package Macro;

import java.util.ArrayList;

import JSON.JSONException;
import JSON.JSONIZABLE;
import JSON.JSONObject;
import Memento.Memento;

public class Macro implements JSONIZABLE{
	
	private ArrayList<Memento>  actions;
	
	public Macro(){
		actions=new ArrayList<Memento>();
	}
	
	public void put(Memento m) {
		actions.add(m);
	}
	public void play(){
		for (int i=0;i< actions.size();i++){
			actions.get(i).execute();
		}
	}
	public void unplay(){
		for (int i=actions.size()-1;i>=0 ;i--){
			actions.get(i).unexecute();
		}
	}

	public String toString(){
		String ret="_{debut Macro}_\n";
		for (int i=0;i<actions.size();i++){
			ret=ret+actions.get(i).MementoToString();
		}
		ret=ret+"_{fin Macro}_";
		return ret;
	}

	public JSONObject JSONize() throws JSONException{
		JSONObject json=new JSONObject();
		for (int i=0;i< actions.size();i++){
			json.accumulate("macro",actions.get(i).JSONize());
		}
		return json;
	}


}
